package com.advitor;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Options;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TestAviator {

    @Test
    public void test(){
        String str = "";
        for(int i=1;i<=2;i+=1){
            str += "(a"+i+"=="+i+" && a"+(i+1)+"=="+(i+1)+") || ";
        }
        str += "include(e_array,e)";
        System.out.println(str);
    }

    @Test
    public void test01() throws IOException {

        {
            //自定义函数
            AviatorEvaluator.addFunction(new Predict());
            //自定义函数
            AviatorEvaluator.addFunction(new PeopleCountLimit());
        }

        long s = System.currentTimeMillis();
        for(int i=0;i<1;i++) {
            long start = System.currentTimeMillis();
            Map<String, Object> env = new HashMap<>();
        /*    for(int m=1;m<=2;m++){
                env.put("a"+m,m+1);
            }*/

            env.put("limitPeopleCount", "1");
            env.put("zhangsan", "1");
           /* env.put("b2", 20);
            env.put("a1",1);
            env.put("a2",2);
            env.put("c", 30);
            env.put("d", 40);
            // e in (1,2,3,4,5,6)
            env.put("e_array", Arrays.asList(1, 2, 3, 4, 5));
            env.put("e", 5);*/

            //String expression = readFile("expression.txt");

            AviatorEvaluator.setOption(Options.TRACE_EVAL, true);
            String a="1";

            //String expression="predict("+a+") == 1";
            String pin="zhang";
            String channelId="san";
            String expression="limitPeopleCount"+"('"+pin+"','"+channelId+"') == 1";
           // String ex="predict("+a+") == 1";
          //  String ex="(a1==1 && a2==2) || include(e_array,e) || predict("+a+")";
            Object result = AviatorEvaluator.compile(expression).execute(env);

            System.out.println(result);

            long end = System.currentTimeMillis();

            System.out.println("t="+(end-start));
        }





    }


    @Test
    public void test02(){

        //多行表达式; 返回最后一个表达式结果
        Object r1 = AviatorEvaluator.execute("println('hello world'); 1+2+3 ; 100-1");
        System.out.println(r1);

        //函数调用
        Object r2 = AviatorEvaluator.execute("string.length('hello')");
        System.out.println(r2);

        //lambda 表达式
        Map<String,Object> data = new HashMap<>();
        data.put("x",10);
        data.put("y",20);

        data.put("map",new HashMap<String,Integer>(){{
            this.put("z",200);
        }});

        data.put("arr",Arrays.asList(1,2,3));

        Object r3 = AviatorEvaluator.execute("(lambda(x,y)->x+y end)(x,y)", data, true);
        System.out.println(r3);


        //自定义函数
        AviatorEvaluator.addFunction(new AddFunction());


        Object r4 = AviatorEvaluator.execute("println('hello'); add(arr[2],map.z)",data);

        Object r5 = AviatorEvaluator.execute("add(add(1,2),3)");

        System.out.println("r4="+r4+"\tr5="+r5);


    }

    public String readFile(String fileName) throws IOException {
        return IOUtils.toString(TestAviator.class.getClassLoader().getResourceAsStream(fileName),"UTF-8");
    }
}
